<%@page import="com.wowangz.cms.common.utils.CodeCreater"%>
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.text.DateFormat"%>
<%@page import="com.wowangz.cms.common.utils.DataFormat"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ page import="com.wowangz.cms.dailyoperation.servicemanage.pojo.Charge"%>
<%@ page import="java.util.List"%>
<%-- 引入页面头 --%>
<%@ include file="/pageHead.jsp"%>
<form name="form1" id="form1" action="${systemctx}/sample/saveSample.web" method="post">
	<div class="pageMain">
		<div class="pageTitle"><%=MessageResourcesUtils.getMessage(request, "systemmanage.servicemanagement.title") %><span></span>
		<%=MessageResourcesUtils.getMessage(request, "servicemessage.services.ordermessage.title1") %><span></span>
		<%=MessageResourcesUtils.getMessage(request, "servicemessage.services.ordermessage.view.title") %></div>
		<div class="pageBody">
				<div class="service-wrap">
				<h3>已购买服务</h3>
				<div class="service-box">
				<%
				List<Charge> servicelist = (List<Charge>)request.getAttribute("servicelist");
				for(Charge charge:servicelist){
				%>
			  	<% if(charge.getIsmonth() == SysConstant.YesOrNo.YES && charge.getIsproposed() == SysConstant.YesOrNo.YES){ %>
					<div class="service-list">
						<table class="service-table">
							<tr>
								<th rowspan="2" width="12%">
									<label>
										<input class="check" type="checkbox" disabled="disabled" checked="checked"/><%=charge.getServicename()%>
									</label>
								</th>
								<td width="24%">
									<label>
										<input class="radio" type="radio" checked="checked" disabled="disabled"/>
										按<b class="ico-vol">笔</b>计费：
										每笔
										<input class="iss_amount4" type="text" id="<%=charge.getServiceid() %>_countamount" name="countamount" value="<%=DataFormat.formatAmount(charge.getProposedprice(),2)%>"/>
										元
									</label>
								</td>
								<td width="12%">
									购买<input class="txt-vol" type="text" id="<%=charge.getServiceid() %>_count" name="count" maxlength="8"/>笔
								</td>
								<td width="35%"></td>
							</tr>
							<tr>
								<td>
									<label>
										<input class="radio" type="radio" checked="checked" disabled="disabled"/>
										按<b class="ico-vol">月</b>计费：
										每月
										<input class="iss_amount4" type="text" id="<%=charge.getServiceid() %>_monamount" name="monamount" value="<%=DataFormat.formatAmount(charge.getMonthprice(),2)%>"/>
										元
									</label>
								</td>
								<td>
									购买<input class="txt-vol" type="text" id="<%=charge.getServiceid() %>_moncount" name="moncount" maxlength="4" value="<%=charge.getBuycount()%>" maxlength="11"/>月
								</td>
								<td>
									开始日期：<input class="iss_datebox" id="<%=charge.getServiceid() %>_startdate" name="startdate" data-options="onSelect:changeSelectFun" type="text" value="<%=charge.getStartdate()%>"/>
									结束日期：<input class="iss_datebox" id="<%=charge.getServiceid() %>_enddate" name="enddate" type="text" value="<%=charge.getEnddate()%>"/>
								</td>
							</tr>
						</table>
					</div>
					<%}else if(charge.getIsmonth() == SysConstant.YesOrNo.YES){ %>
					<div class="service-list">
						<table class="service-table">
							<tr>
								<th rowspan="2" width="12%">
									<label>
										<input class="check" type="checkbox" disabled="disabled" checked="checked"/><%=charge.getServicename()%>
									</label>
								</th>
								<td width="24%">
									<label>
										<input class="radio" type="radio" checked="checked" disabled="disabled"/>
										按<b class="ico-vol">月</b>计费：
										每月
										<input class="iss_amount4" type="text" id="<%=charge.getServiceid() %>_monamount" name="monamount" value="<%=DataFormat.formatAmount(charge.getMonthprice(),2)%>"/>
										元
									</label>
								<td width="12%">
									购买<input class="txt-vol" type="text" id="<%=charge.getServiceid() %>_moncount" name="moncount" maxlength="4" value="<%=charge.getBuycount()%>"/>月</td>
								<td width="35%">
									开始日期：<input type="text" class="iss_datebox" id="<%=charge.getServiceid() %>_startdate" name="startdate" data-options="onSelect:changeSelectFun" value="<%=charge.getStartdate()%>"/>
									结束日期：<input class="iss_datebox" id="<%=charge.getServiceid() %>_enddate" name="enddate" type="text" value="<%=charge.getEnddate()%>"/>
								</td>
							</tr>
							<tr>
								<td></td>
								<td></td>
								<td></td>
							</tr>
						</table>
					</div>
					<%}else if(charge.getIsproposed() == SysConstant.YesOrNo.YES){ %>
					<div class="service-list">
						<table class="service-table">
							<tr>
								<th rowspan="2" width="12%">
									<label>
										<input class="check" disabled="disabled" checked="checked" type="checkbox"/><%=charge.getServicename()%>
									</label>
								</th>
								<td width="24%">
									<input class="radio" type="radio" checked="checked" disabled="disabled">
									按<b class="ico-vol">笔</b>计费：
									每笔
									<input class="iss_amount4" type="text" id="<%=charge.getServiceid() %>_countamount" name="countamount" value="<%=DataFormat.formatAmount(charge.getProposedprice(),2)%>"/>
									元
								</td>
								<td width="12%">
									购买<input class="txt-vol" type="text" id="<%=charge.getServiceid() %>_count" maxlength="8" name="count" value="<%=charge.getBuycount()%>"/>笔</td>
								<td width="35%"></td>
							</tr>
							<tr>
								<td></td>
								<td></td>
								<td></td>
							</tr>
						</table>
					</div>
				</div>
					<%} %>
			<%}%>
				<div class="total-box heji">
					<h4>按笔合计：</h4>
					<ul>
						<li><span class="vol" id="count_num">${countNum}</span>笔</li>
						<li>共<span class="price" id="count_price">${countAmount}</span>元
						</li>
					</ul>
				</div>
				<div class="total-box heji">
					<h4>按月合计：</h4>
					<ul>
						<li><span class="vol" id="month_num">${monthNum}</span>月</li>
						<li>共<span class="price" id="month_price">${monthAmount}</span>元
						</li>
					</ul>
				</div>
				<div class="total-box zongji">
					<h4>总计：</h4>
					<ul>
						<li><span id="total_price">${totalAmount}</span>元</li>
					</ul>
				</div>
			</div>	
		</div>
		<div class="button-group">
			<input type="hidden" id="storeid" name="storeid" value="${storeid}" />
			<input type="button" class="btn btn-default" id="butSubmit" name="butSubmit" value="<%=MessageResourcesUtils.getMessage(request, "common.button.pay")%>" />
			<input type="button" class="btn btn-default" id="butBack" name="butBack" value="<%=MessageResourcesUtils.getMessage(request, "common.button.back")%>" />
		</div>	
	</div>
</form>
<script type="text/javascript">
$(document).ready(function(){
	$('#butBack').click(function() {
		parent.document.location.reload();
		$.fn.closeIssWindow();
	});
	$('#butSubmit').click(function(){
		if(validateSubmitData()){
			var data="";
			$('input[class="txt-vol"]').each(function(i,val){
				var id=val.id.split("_")[0];
				var value=val.id.split("_")[1];
				//验证 收费方式
				if(value=="count"){
					//按笔数
					var countval=$("#"+id+"_count").val();
					var countamount=$("#"+id+"_countamount").val();
					if(countval!=null && countval!='' && countval!='undefined' && 
							typeof(countval)!='undefined' && typeof(countval)!=undefined){
						if(countval > 0){
							if(validataInputNum(countval)){
								data=data+"<%=SysConstant.ChargeManage.TRADESUM%>:"+id+":"+countval+":"+countamount+"##";
							}
						}
					}
				}else if(value=="moncount"){
					var monthval=$("#"+id+"_moncount").val();
					var monthamount=$("#"+id+"_monamount").val();
					if(monthval!=null && monthval!='' && monthval!='undefined' && typeof(monthval)!='undefined' && typeof(monthval)!=undefined){
						if(monthval > 0){
							if(validataInputNum(monthval)){
								var startdate = $('#'+id+"_startdate").datebox('getValue');
								if(startdate!=null && startdate!=''){
									data=data+"<%=SysConstant.ChargeManage.MONTH%>:"+id+":"+monthval+":"+monthamount+":"+startdate+"##";
								}
							}
						}
					}
				}
			});
			if(data!=null && data!=''){
				if(confirm('<%=MessageResourcesUtils.getMessage(request, "servicemessage.services.ordermessage.confirm1")%>')){
					submitData(data);
				}
			}
		}
	});
});
function validateSubmitData(){
	var resFlag=false;
	$('input[class="txt-vol"]').each(function(){
		var id=this.id.split("_")[0];
		var value=this.id.split("_")[1];
		if(value=="moncount"){
			//按月
			var monthval=$("#"+id+"_moncount").val();
			if(monthval!=null && monthval!='' && monthval!='undefined' && 
					typeof(monthval)!='undefined' && typeof(monthval)!=undefined){
				if(monthval == 0){
					alert("按月计费,购买月数不能为0!");
					resFlag= false;
					return false
				}else{
					if(validataInputNum(monthval)){
						var startdate = $('#'+id+"_startdate").datebox('getValue');
						if(startdate!=null && startdate!=''){
							resFlag= true;
						}else{
							alert("按月计费,购买开始日期不能为空!");
							resFlag= false;
							return false
						}
					}else{
						alert("按月计费,购买月数只能为正整数!");
						resFlag= false;
						return false
					}
				}
				
			}else{
				alert("按月计费,购买月数不能为空!");
				resFlag= false;
				return false
			}
		}else if(value=="count"){
			//按笔数
			var countval=$("#"+id+"_count").val();
			if(countval!=null && countval!='' && countval!='undefined' && 
					typeof(countval)!='undefined' && typeof(countval)!=undefined){
				if(countval == 0){
					alert("按笔计费,购买笔数不能为0!");
					resFlag=false;
					return false
				}else{
					if(validataInputNum(countval)){
						resFlag= true;
					}else{
						alert("按笔计费,购买笔数只能为正整数!");
						resFlag= false;
						return false
					}
				}
			}else{
				alert("按笔计费,购买笔数不能为空!");
				resFlag= false;
				return false
			}
		}else{
			alert("计费方式不正确!");
			resFlag= false;
			return false
		}
	});
	return resFlag;
}
function submitData(data){
	showSending();
	var storeid = $('#storeid').val();
	$.ajax({
		type : 'POST',
		url : '${systemctx}/dailyoperation/servicemanage/modifySaveService.json?storeid='+storeid,
		data : {type:data,tranCode:'${transcode}',mainid:'${mainid}'},
		dataType : 'json',
		success : function(data) {
			//显示AJAX请求后的提示信息
			closeSending();
			if(data.messagetype==1){
				alert(data.message);
				location.reload();
			}else{
				alert(data.message);
			}
		}
	});
}
//金额变化
$('input[class="iss_amount4"]').bind('change',function(){
	if(validataInputAmountNum(this.value)){
		changeInputMonthOrCount(this.id);
	}else{
		$("#"+this.id).val('0.00');
		changeInputMonthOrCount(this.id);
	}
});
//笔数 月数变化
$('input[class="txt-vol"]').bind('change',function(){
	if(validataInputNum(this.value)){
		changeInputMonthOrCount(this.id);
		//判断是否是月数变动
		var monValue=this.id.split("_")[1];
		if(monValue=="moncount"){
			changeDateSelectFunById(this.id);
		}
	}else{
		$("#"+this.id).val('0');
		changeInputMonthOrCount(this.id);
		alert('<%=MessageResourcesUtils.getMessage(request, "servicemessage.services.ordermessage.title5")%>');
	}
});
function changeDateSelectFunById(valid){
	var val=valid.split("_")[0]
	if(document.getElementById(val+"_startdate")!=undefined){
		var dateval=$("#"+val+"_startdate").datebox('getValue');
		var countval=$("#"+val+"_moncount").val();
		if(dateval!=null && dateval!='' && dateval!='undefined' && typeof(dateval)!='undefined' && typeof(dateval)!=undefined &&
			 countval!=null && countval!='' && countval!='undefined' && typeof(countval)!='undefined' && typeof(countval)!=undefined	){
			addCurMonth(dateval,countval,val);
		}
	}
}
function changeInputMonthOrCount(obj){
	var totalCountAmount=0.00,totalMonthAmount=0.00;
	var countNum=0,monthNum=0;
	$('input[class="iss_amount4 numberbox-f validatebox-text"]').each(function(){
		var val=this;
		totalCountAmount+=new Number($.reverseFormatAmount(calculationCountAmount(val.id)));
		totalMonthAmount+=new Number($.reverseFormatAmount(calculationMonthAmount(val.id)));
		countNum+=new Number(calculationCountNum(val.id));
		monthNum+=new Number(calculationMonthNum(val.id));
	});
	var totailAmount=new Number($.reverseFormatAmount(totalCountAmount))+new Number($.reverseFormatAmount(totalMonthAmount));
	$("#count_num").html(countNum);
	$("#count_price").html($.formatAmount(totalCountAmount,2));
	$("#month_num").html(monthNum);
	$("#month_price").html($.formatAmount(totalMonthAmount,2));
	$("#total_price").html($.formatAmount(totailAmount,2));
}
//验证输入的 笔数或月数  是否为正整数
function validataInputNum(val){
	var tt=/^\d+$/g;
	if(tt.test(val)){
		return true;
	}else{
		return false;
	}
}
function validataInputAmountNum(val){
	var a=/^[0-9]*(\.[0-9]{1,2})?$/;
	if(!a.test(val)){
		alert('<%=MessageResourcesUtils.getMessage(request, "servicemessage.services.ordermessage.title5")%>');
		return false;
	}else{
		var part = String(val).split(".");
		if(part[0].length >6){
			alert('金额整数位最大支持6位，请重新输入!');
			return false;
		}else{
			return true;
		}
	}
}
//日期变更  
function changeSelectFun(date){
	var dateid=this.id.split("_")[0];
	var len=(date.getMonth()+1);
	if(len<10){
		len='0'+len;
	}
	var day=date.getDate();
	if(date.getDate()<10){
		day='0'+day;
	}
	var dateval=date.getFullYear()+"/"+len+"/"+day;
	var curdate='<%=new SimpleDateFormat("yyyy/MM/dd").format(new Date())%>';
	var monthval=$("#"+dateid+"_moncount").val();
	if(validataInputNum(monthval)){
		dateval=date.getFullYear()+"-"+len+"-"+day;
		addCurMonth(dateval,monthval,dateid);
	}
}
//验证输入的 笔数或月数  是否为正整数
function validataInputNum(val){
	var tt=/^\d+$/g;
	if(tt.test(val)){
		return true;
	}else{
		return false;
	}
}
function addCurMonth(sDate,num,dateid) {
	$.ajax({
		type: 'POST',
		url: '${systemctx}/dailyoperation/servicemanage/queryServicebuyForDate.json',
		data: {startdate:sDate,num:num},
		dataType: 'json',
		success: function (data) {
			if(data!=null && data!=''){
				$("#"+dateid+"_enddate").datebox('setValue',data.info);	
			}
		}
	});
}
//计算 总笔数
function calculationCountNum(valid){
	var totalAmount=0,totalCount=0;
	//按笔数计算
	var val=valid.split("_")[0]
	var countval=$("#"+val+"_count").val();
	if(countval!=null && countval!='' && countval!='undefined' && typeof(countval)!='undefined' && typeof(countval)!=undefined){
		if(validataInputNum(countval)){
			totalCount=new Number(countval);//笔数
		}else{
			totalCount=0;
		}
	}else{
		totalCount=0;
	}
	totalAmount=new Number(totalCount);//笔数总金额
	return totalAmount;
}
//计算 总月数
function calculationMonthNum(valid){
	var totalMonth=0;
	//按月数计算
	var val=valid.split("_")[0]
	var monthval=$("#"+val+"_moncount").val();
	if(monthval!=null && monthval!='' && monthval!='undefined' && typeof(monthval)!='undefined' && typeof(monthval)!=undefined){
		if(validataInputNum(monthval)){
			totalMonth=new Number(monthval);//月数
		}else{
			totalMonth=0;
		}
	}else{
		totalMonth=0;
	}
	return new Number(totalMonth);//总月数
}
//计算金额 按笔数  返回金额值
function calculationCountAmount(valid){
	var totalAmount=0.00,totalCount=0.00;
	//按笔数计算
	//获得单选按钮值
	var val=valid.split("_")[0]
	var radpreval=$("#"+val+"_countamount").val();
	var countval=$("#"+val+"_count").val();
	if(radpreval!=null && radpreval!='' && radpreval!='undefined' && typeof(radpreval)!='undefined' && typeof(radpreval)!=undefined &&
		countval!=null && countval!='' && countval!='undefined' && typeof(countval)!='undefined' && typeof(countval)!=undefined){
		if(validataInputNum(countval)){
			var countAmount=new Number($.reverseFormatAmount(radpreval));//笔数金额
			var countNum=new Number($.reverseFormatAmount(countval));//笔数
			totalCount=countAmount.mul(countNum);
		}else{
			totalCount=0.00;
		}
	}else{
		totalCount=0.00;
	}
	totalAmount=new Number($.reverseFormatAmount(totalCount));//笔数总金额
	return totalAmount;
}
//计算金额 按月数  返回金额值
function calculationMonthAmount(valid){
	var totalAmount=0.00,totalMonth=0.00;
	//按月数计算
	var val=valid.split("_")[0]
	var radmonval=$("#"+val+"_monamount").val();
	var monthval=$("#"+val+"_moncount").val();
	if(radmonval!=null && radmonval!='' && radmonval!='undefined' && typeof(radmonval)!='undefined' && typeof(radmonval)!=undefined &&
	   monthval!=null && monthval!='' && monthval!='undefined' && typeof(monthval)!='undefined' && typeof(monthval)!=undefined){
		if(validataInputNum(monthval)){
			var monthAmount=new Number($.reverseFormatAmount(radmonval));//月金额
			var monthNum=new Number($.reverseFormatAmount(monthval));//月数
			totalMonth=monthAmount.mul(monthNum);
		}else{
			totalMonth=0.00;
		}
	}else{
		totalMonth=0.00;
	}
	totalAmount=new Number($.reverseFormatAmount(totalMonth));//月数总金额
	return totalAmount;
}
</script>
<%-- 引入页面尾 --%>
<%@ include file="/pageFloor.jsp"%>